<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>localStorage和sessionStorage</title>
</head>
<body>
    <h1>localStorage</h1>
    <button onclick="setData()">保存数据</button>
    <button onclick="getData()">获取数据</button>
    <button onclick="removeData()">删除数据</button>
    <button onclick="clearData()">清空数据</button>

    <script>
        // sessionStorage同样有这四个方法：setItem、getItem、removeItem、clear
        
        let p = { name: '张三', age: 18 }

        function setData() {
            localStorage.setItem('msg', 'hello!!!!')
            localStorage.setItem('msg2', 666)
            localStorage.setItem('person', JSON.stringify(p))
        }
        function getData() {
            console.log(localStorage.getItem('msg'))
            console.log(localStorage.getItem('msg2'))
            console.log(JSON.parse(localStorage.getItem('person')))
        }
        function removeData() {
            localStorage.removeItem('msg2')
        }
        function clearData() {
            localStorage.clear()
        }
    </script>
</body>
</html>